//
//  InformationViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/23.
//

#import "InformationViewController.h"

@interface InformationViewController ()<TZImagePickerControllerDelegate>
@property(nonatomic,strong) UIImageView * avatarImage;
@end

@implementation InformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHideNavigationBar = YES;
    self.view.backgroundColor = BACKGROUNDCOLOR;
}

- (void)initSubviews {
    [super initSubviews];
 
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 230)];
    [bgView az_setGradientBackgroundWithColors:@[HexColor(@"#C4EFEC"),HexColor(@"#F1F4F4")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1)];
    [self.view addSubview:bgView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:UIImageMake(@"icon_arrow_left") forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(15, StatusBarHeightConstant, 40, 40);
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myHorzMargin = 15;
    root.myTop = NavigationContentTop;
    root.subviewVSpace = 10;
    [self.view addSubview:root];
    
    MyLinearLayout *top = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    top.myHorzMargin = 0;
    top.gravity = MyGravity_Vert_Center;
    top.myHeight = 108;
    [root addSubview:top];
    
    {
        MyLinearLayout *left = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
        left.weight = 1;
        left.myHeight = 108;
        left.subviewVSpace = 15;
        [top addSubview:left];
        
        {
            UILabel *name = [UILabel createLabelText:@"完善资料" font:UIFontBoldMake(24) textColor:COLOR1515];
            name.myTop = 10;
            name.mySize = kAutoSize;
            [left addSubview:name];
            
            UILabel *detail = [UILabel createLabelText:@"我们还需要其他一些资料来提高健康大数据计算系统的精确度" font:UIFontMake(14) textColor:HexColor(@"#91AFAE")];
            detail.myHorzMargin = 0;
            detail.myHeight = kAutoHeight;
            [left addSubview:detail];
        }
        
        UIImageView *rightImage = [[UIImageView alloc]initWithImage:UIImageMake(@"login_ziliao")];
        rightImage.mySize = CGSizeMake(108, 108);
        [top addSubview:rightImage];
    }
    
    MyLinearLayout *content = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    content.myHorzMargin = 0;
    content.myTop = 0;
    content.padding = UIEdgeInsetsMake(20, 15, 0, 15);
    content.backgroundColor = COLOR_4;
    ViewCorner(content, 15);
    content.gravity = MyGravity_Horz_Center;
    [root addSubview:content];
    
    {
        
        UIImageView *avatarImage = [[UIImageView alloc]initWithImage:UIImageMake(@"avatar")];
        avatarImage.mySize = CGSizeMake(64, 64);
        ViewCorner(avatarImage, 32);
        avatarImage.userInteractionEnabled = YES;
        [content addSubview:avatarImage];
        self.avatarImage = avatarImage;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(avatarImageClick)];
        [avatarImage addGestureRecognizer:tap];
        
        UILabel *lb = [UILabel createLabelText:@"上传头像" font:UIFontMake(14) textColor:COLOR666];
        lb.mySize = kAutoSize;
        lb.myTop = 10;
        [content addSubview:lb];
        
        
        NSArray *array = @[
        @{@"name":@"昵称",@"content":@"请输入昵称"},
        @{@"name":@"性别",@"content":@"请选择性别"},
        @{@"name":@"生日",@"content":@"请选择生日"},
        @{@"name":@"身高",@"content":@"请选择身高"},
        @{@"name":@"体重",@"content":@"请选择体重"},];
        for (int i = 0; i < array.count; i++) {
            [content addSubview:[self setAddLineTop:i==0?20:0]];
            
            MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
            item.myHorzMargin = 0;
            item.gravity = MyGravity_Vert_Center;
            item.myHeight = 50;
            item.subviewHSpace = 10;
            item.tag = 100 + i;
            [content addSubview:item];
            [item setTarget:self action:@selector(itemClick:)];

            {
                UILabel *name = [UILabel createLabelText:array[i][@"name"] font:UIFontMake(15) textColor:COLOR1515];
                name.mySize = kAutoSize;
                name.myWidth = 100;
                [item addSubview:name];
                
                QMUITextField * tf = QMUITextField.new;
                
                tf.weight = 1;
                NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:array[i][@"content"] attributes:
                                                             @{NSForegroundColorAttributeName:HexColor(@"#BABABA"),
                                                               NSFontAttributeName:UIFontMake(15)}
                ];
                tf.attributedPlaceholder = attrString;
                tf.myHeight = 30;
                tf.enabled = i==0?YES:NO;
                tf.maximumTextLength = i == 0 ? 8 : 12;
                tf.tag = 1000 + i;
                [item addSubview:tf];
                
                if(i != 0){
                    UIImageView *right = [[UIImageView alloc]initWithImage:UIImageMake(@"icon_arrow_right")];
                    right.mySize = CGSizeMake(16, 16);
                    [item addSubview:right];
                }
            }
            
            
            
        }
    }
    
    UIButton *submitBtn = [UIButton createButtonText:@"提交" font:UIFontMake(14) textColor:COLOR_4];
    submitBtn.myHorzMargin = 0;
    submitBtn.myTop = 20;
    submitBtn.myHeight = 46;
    ViewCorner(submitBtn, 23);
    [submitBtn az_setGradientBackgroundWithColors:@[HexColor(@"#4BD8B1"),HexColor(@"#2CAED8")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 1)];
    [submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [root addSubview:submitBtn];
}

- (void)itemClick:(UIView *)view {
    [self.view endEditing: YES];
    switch (view.tag - 100) {
        case 1:
        {
            [BRStringPickerView showPickerWithTitle:@"请选择性别" dataSourceArr:@[@"男",@"女"] selectIndex:0 resultBlock:^(BRResultModel * _Nullable resultModel) {
                QMUITextField *tf = (QMUITextField *)[self.view viewWithTag:1001];
                tf.text = resultModel.value;
            }];
        }
            break;
        case 2:
        {
            [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:@"请选择生日" selectValue:@"2000-01-01" resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
                QMUITextField *tf = (QMUITextField *)[self.view viewWithTag:1002];
                tf.text = selectValue;
            }];
        }
            break;
        case 3:
        {
            NSMutableArray *arr = @[].mutableCopy;
            NSInteger first = 150;
            for (int i = 0; i < 11; i++) {
                [arr addObject:FormatString(@"%ldcm",first)];
                first+= 5;
            }
            [BRStringPickerView showPickerWithTitle:@"身高修改" dataSourceArr:arr.copy selectIndex:0 isAutoSelect:NO resultBlock:^(BRResultModel * _Nullable resultModel) {
                QMUITextField *tf = (QMUITextField *)[self.view viewWithTag:1003];
                tf.text = resultModel.value;
            }];
        }
            break;
        case 4:
        {
            NSMutableArray *arr = @[].mutableCopy;
            NSInteger first = 40;
            for (int i = 0; i < 13; i++) {
                [arr addObject:FormatString(@"%ldkg",first)];
                first+= 5;
            }
            [BRStringPickerView showPickerWithTitle:@"体重修改" dataSourceArr:arr.copy selectIndex:0 isAutoSelect:NO resultBlock:^(BRResultModel * _Nullable resultModel) {
                QMUITextField *tf = (QMUITextField *)[self.view viewWithTag:1004];
                tf.text = resultModel.value;
            }];
        }
            break;
            
        default:
            break;
    }
}

- (void)submitBtnClick {
    
    [self.view endEditing:YES];
    
    QMUITextField * nickName = (QMUITextField *)[self.view viewWithTag:1000];
    if(nickName.text.length ==  0){
        [QMUITips showError:@"请输入昵称"];
        return;
    }
    
    QMUITextField * sex = (QMUITextField *)[self.view viewWithTag:1001];
    if(sex.text.length ==  0){
        [QMUITips showError:@"请选择性别"];
        return;
    }
    
    QMUITextField * brithday = (QMUITextField *)[self.view viewWithTag:1002];
    if(brithday.text.length ==  0){
        [QMUITips showError:@"请选择生日"];
        return;
    }
    
    QMUITextField * height = (QMUITextField *)[self.view viewWithTag:1003];
    if(height.text.length ==  0){
        [QMUITips showError:@"请选择身高"];
        return;
    }
    
    QMUITextField * weight = (QMUITextField *)[self.view viewWithTag:1004];
    if(weight.text.length ==  0){
        [QMUITips showError:@"请选择身高"];
        return;
    }
    
}

#pragma mark - 选择图片上传

- (void)avatarImageClick {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:3 delegate:self pushPhotoPickerVc:YES];

    imagePickerVc.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];
    imagePickerVc.showPhotoCannotSelectLayer = YES;
    imagePickerVc.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    // 3. 设置是否可以选择视频/图片/原图
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingOriginalPhoto = YES;
    imagePickerVc.statusBarStyle = UIStatusBarStyleLightContent;
       // 设置是否显示图片序号
    imagePickerVc.showSelectedIndex = YES;
    
    imagePickerVc.allowCrop = YES;
    imagePickerVc.needCircleCrop = YES;
    imagePickerVc.circleCropRadius = kScreenWidth/2;
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
    UIImage *resultImage = photos.firstObject;
    
    self.avatarImage.image = resultImage;
    
}
@end
